﻿@{
    ViewBag.Title = "Състезания";
    const string ContestControllerName = "Contests";
    const string ContestQuestionControllerName = "ContestQuestions";
    const string ContestQuestionAnswersControllerName = "ContestQuestionAnswers";
}
<h1>@ViewBag.Title</h1>

@(Html.Kendo().Grid<OJS.Web.Areas.Administration.ViewModels.Contest.ContestAdministrationViewModel>()
    .Name("DataGrid")
    .Columns(columns =>
        {
            columns.Bound(x => x.Id);
            columns.Bound(x => x.Name);
            columns.Bound(x => x.StartTime).Format("{0:dd/MM/yyyy HH:mm}");
            columns.Bound(x => x.EndTime).Format("{0:dd/MM/yyyy HH:mm}");
            columns.Bound(x => x.PracticeStartTime).Format("{0:dd/MM/yyyy HH:mm}");
            columns.Bound(x => x.PracticeEndTime).Format("{0:dd/MM/yyyy HH:mm}").Hidden();
            columns.Bound(x => x.IsVisible).Hidden();
            columns.Bound(x => x.CreatedOn).Format("{0:dd/MM/yyyy HH:mm}").Hidden();
            columns.Bound(x => x.ModifiedOn).Format("{0:dd/MM/yyyy HH:mm}").Hidden();
            columns.Template(@<text></text>).ClientTemplate("<a class='k-button' href='" + Url.Action("Solutions", "ContestsExport") + "/#=Id#?compete=true'>На състезанието</a><br /><a class='k-button' href='" + Url.Action("Solutions", "ContestsExport") + "/#=Id#?compete=false'>На упражненията</a>").Title("Решения").Width(200);
            columns.Template(@<text></text>).ClientTemplate("<a class='k-button' href='" + Url.Action("Results", "ContestsExport") + "/#=Id#?compete=true'>На състезанието</a><br /><a class='k-button' href='" + Url.Action("Results", "ContestsExport") + "/#=Id#?compete=false'>На упражненията</a>").Title("Класиране").Width(200);
            columns.Template(@<text></text>).ClientTemplate("<a class='k-button' href='" + Url.Action("Contest", "Problems") + "/#=Id#'>Задачи</a><br /><a class='k-button' href='" + Url.Action("Contest", "Participants") + "/#=Id#'>Участници</a>").Title("Други");
            columns.Template(@<text></text>).ClientTemplate("<a class='k-button' href='" + Url.Action("Edit", "Contests") + "/#=Id#'>Промени</a>").Title("Промяна");
            columns.Command(command => command.Destroy().Text(" ")).Width(80).Title("Изтриване");
    })
    .ToolBar(toolbar =>
    {
        toolbar.Custom().Text("Създай").Action("Create", ContestControllerName);
        toolbar.Custom().Text("Обратно към администрацията").Action("Navigation", "Administration", new { Area = "" }).Name("custom-toolbar-button");
        toolbar.Custom().Text("Експорт към Excel").Name("custom-toolbar-button").HtmlAttributes(new { id = "export" }).Url(Url.Action("ExportToExcel", ContestControllerName, new { page = 1, pageSize = "~", filter = "!!", sort = "~" }));
    })
    .Editable(editable =>
    {
        editable.Mode(GridEditMode.PopUp);
        editable.Window(w => w.Title(ViewBag.Title));
        editable.DisplayDeleteConfirmation("Наистина ли искате да изтриете елемента?");
    })
    .ColumnMenu()
    .ClientDetailTemplateId("questions-template")
    .Events(e => e.DataBound("onDataBound"))
    .Pageable(x => x.Refresh(true))
    .Sortable(x => x.Enabled(true).AllowUnsort(false))
    .Filterable(x => x.Enabled(true))
    .Groupable(x =>
    {
        x.Enabled(true);
        x.Messages(m => m.Empty("Хванете заглавието на колона и го преместете тук, за да групирате по тази колона."));
    })
    .Reorderable(x => x.Columns(true))
    .Resizable(x => x.Columns(true))
    .DataSource(datasource => datasource
        .Ajax()
        .ServerOperation(true)
        .Model(model => model.Id(x => x.Id))
        .Sort(sort => sort.Add(x => x.CreatedOn).Descending())
        .Create(create => create.Action("Create", ContestControllerName))
        .Read(read => read.Action("Read", ContestControllerName))
        .Update(update => update.Action("Update", ContestControllerName))
        .Destroy(destroy => destroy.Action("Destroy", ContestControllerName))
        .Events(ev => ev.Error("validateModelStateErrors"))
    )
)

<script type="text/javascript">
    function onDataBound() {
        CreateExportToExcelButton();
    }
</script>

<script type="text/x-kendo-template" id="questions-template">
    @(Html.Kendo().Grid<OJS.Web.Areas.Administration.ViewModels.ContestQuestion.ContestQuestionViewModel>()
        .Name("DetailQuestionsGrid_#=Id#")
        .Columns(columns =>
        {
            columns.Bound(m => m.QuestionId);
            columns.Bound(m => m.Text);
            columns.Bound(m => m.Type).ClientTemplate("\\#= getTypeName(Type) \\#");
            columns.Bound(m => m.RegularExpressionValidation).Hidden();
            columns.Bound(m => m.AskPracticeParticipants);
            columns.Bound(m => m.AskOfficialParticipants);
            columns.Bound(m => m.CreatedOn).Hidden();
            columns.Bound(m => m.ModifiedOn).Hidden();
            columns.Command(com => com.Edit().Text(" ")).Width(80);
            columns.Command(com => com.Destroy().Text(" ")).Width(80);
        })
        .ToolBar(tool =>
            {
                tool.Create().Text("Добави въпрос");
                tool.Custom().Text("Копирай от...").HtmlAttributes(new { onclick = "copyFromContest(event, #=Id#)" });
            })
        .ColumnMenu()
        .ClientDetailTemplateId("answers-template")
        .Editable(edit =>
            {
                edit.Mode(GridEditMode.PopUp);
                edit.Window(w => w.Title(ViewBag.Title));
                edit.DisplayDeleteConfirmation("Наистина ли искате да изтриете елемента?");
            })
        .DataSource(data =>
        {
            data.Ajax()
                .ServerOperation(true)
                .Model(model =>
                {
                    model.Id(m => m.QuestionId);
                })
                .Sort(sort => sort.Add(field => field.CreatedOn))
                .Create(create => create.Action("AddQuestionToContest", ContestQuestionControllerName, new { id = "#= Id #" }))
                .Read(read => read.Action("QuestionsInContest", ContestQuestionControllerName, new { id = "#= Id #" }))
                .Update(update => update.Action("UpdateQuestionInContest", ContestQuestionControllerName))
                .Destroy(destroy => destroy.Action("DeleteQuestionFromContest", ContestQuestionControllerName));
        })
        .ToClientTemplate())

    @(Html.Kendo().Window()
        .Name("CopyFromContestWindow_#=Id#")
        .Title("Копирай от друго състезание")
        .LoadContentFrom("/Administration/ContestQuestions/CopyFromAnotherContest/#=Id#")
        .Visible(false)
        .Modal(true)
        .Width(400)
        .Actions(actions => actions.Close())
        .ToClientTemplate())
</script>

<script type="text/x-kendo-template" id="answers-template">
    # if ((Type == 0) || (Type == 1)) { #
    @(Html.Kendo().Grid<OJS.Web.Areas.Administration.ViewModels.ContestQuestionAnswer.ContestQuestionAnswerViewModel>()
        .Name("DetailAnswerGrid_#=QuestionId#")
        .Columns(columns =>
        {
            columns.Bound(m => m.AnswerId);
            columns.Bound(m => m.Text);
            columns.Bound(m => m.QuestionText);
            columns.Command(com => com.Edit().Text(" ")).Width(80);
            columns.Command(com => com.Destroy().Text(" ")).Width(80);
        })
        .ToolBar(tool =>
            {
                tool.Create().Text("Добави отговор");
                tool.Custom().Text("Изтрий всички").HtmlAttributes(new { id = "delete-all-answers", data_question_id = "#=QuestionId#" });
            })
        .ColumnMenu()
        .Editable(edit =>
            {
                edit.Mode(GridEditMode.PopUp);
                edit.Window(w => w.Title(ViewBag.Title));
                edit.DisplayDeleteConfirmation("Наистина ли искате да изтриете елемента?");
            })
        .DataSource(data =>
        {
            data.Ajax()
                .ServerOperation(true)
                .Model(model =>
                {
                    model.Id(m => m.QuestionId);
                })
                .Sort(sort => sort.Add(field => field.AnswerId))
                .Create(create => create.Action("AddAnswerToQuestion", ContestQuestionAnswersControllerName, new { id = "#= QuestionId #" }))
                .Read(read => read.Action("AnswersInQuestion", ContestQuestionAnswersControllerName, new { id = "#= QuestionId #" }))
                .Update(update => update.Action("UpdateAnswerInQuestion", ContestQuestionAnswersControllerName))
                .Destroy(destroy => destroy.Action("DeleteAnswerFromQuestion", ContestQuestionAnswersControllerName, new { id = "#= QuestionId #" }));
        })
        .ToClientTemplate())
    # }
    else { #
    <div class="text-center">Избраният тип въпрос няма възможни отговори!</div>
    # } #
</script>

@section scripts {
    <script src="~/Scripts/Administration/Contests/contests-index.js"></script>
}
